<?php

class Form_Field_Select_Exception extends Form_Field_Exception {}
class Form_Field_Select_Incomplete_Exception extends Form_Field_Select_Exception {}

class Form_Field_Select extends Form_Field {

	protected $value;
	protected $options = array();

	public function value($value){
		$this->value = $value;
	}

	public function getValue(){
		return $this->value;
	}

	public function optgroup($value, $text){
		$this->options[$value]['label'] = $text;
	}

	public function option($value, $text, $optgroup = ''){
		$this->options[$optgroup]['options'][$value] = $text;
	}

	public function output($base){
		$tpl = Output::getInstance()->Template(array('form','field','select'));
		$tpl->title = $this->title;
		$tpl->value = $this->value;
		$tpl->options = $this->options;
		$base[] = $this->name;
		$tpl->name = $base;
		return parent::output($base, $tpl);
	}

	public function run($value){
		if(!isset($value)){
			throw new Form_Field_Select_Incomplete_Exception();
		}
		$this->value = $value;
	}

	public function data(){
		return $this->value;
	}


}
